package jzoffer;

import java.util.PriorityQueue;

/**
 * @author MaoLin Wang
 * @date 2020/10/13 4:13 下午
 */
public class _40最小的k个数{
    public static int[] getLeastNumbers(int[] arr, int k) {
        if (arr==null || k<=0){
            return new int[0];
        }
        PriorityQueue<Integer>priorityQueue=new PriorityQueue<Integer>(arr.length);
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }
        int[]res=new int[k];
        int index=0;
        for (int i = 0; i < k; i++) {
            res[index++]=priorityQueue.poll();
        }

        return res;
    }

    private static int leftChild(int i) {
        return i*2+1;
    }

    public static void main(String[] args) {
        int[] leastNumbers = getLeastNumbers(new int[]{3, 2, 1}, 2);
        for (int leastNumber : leastNumbers) {
            System.out.println(leastNumber);
        }
    }
}
